import 'package:fish_redux/fish_redux.dart';
import '../../admin_user_list_page/admin_user_item_component/component.dart';
import '../../admin_user_list_page/admin_user_item_component/state.dart';

import '../state.dart';
import 'reducer.dart';

class AdminUserListAdapter extends DynamicFlowAdapter<AdminUserListState> {
  AdminUserListAdapter()
      : super(
          pool: <String, Component<Object>>{
            'admin_user_item': AdminUserItemComponent()
          },
          connector: _AdminUserListConnector(),
          reducer: buildReducer(),
        );
}

class _AdminUserListConnector
    extends ConnOp<AdminUserListState, List<ItemBean>> {
  @override
  List<ItemBean> get(AdminUserListState state) {
    if (state.list?.isNotEmpty == true) {
      return state.list
          .map<ItemBean>(
              (AdminUserItemState info) => ItemBean('admin_user_item', info))
          .toList(growable: true);
    }

    return <ItemBean>[];
  }

  @override
  void set(AdminUserListState state, List<ItemBean> items) {
    if (items?.isNotEmpty == true) {
      state.list = List<AdminUserItemState>.from(
          items.map<AdminUserItemState>((ItemBean bean) => bean.data).toList());
    } else {
      state.list = <AdminUserItemState>[];
    }
  }

  @override
  subReducer(reducer) {
    return super.subReducer(reducer);
  }
}
